var T = Object.defineProperty;
var E = (u, n, e) =>
  n in u
    ? T(u, n, { enumerable: !0, configurable: !0, writable: !0, value: e })
    : (u[n] = e);
var m = (u, n, e) => (E(u, typeof n != 'symbol' ? n + '' : n, e), e);
import { _defaults as S } from './defaults.js';
import {
  rtrim as d,
  splitCells as B,
  escape as f,
  findClosingBracket as M,
} from './helpers.js';
function C(u, n, e, t) {
  const i = n.href,
    s = n.title ? f(n.title) : null,
    l = u[1].replace(/\\([\[\]])/g, '$1');
  if (u[0].charAt(0) !== '!') {
    t.state.inLink = !0;
    const o = {
      type: 'link',
      raw: e,
      href: i,
      title: s,
      text: l,
      tokens: t.inlineTokens(l),
    };
    return (t.state.inLink = !1), o;
  }
  return {
    type: 'image',
    raw: e,
    href: i,
    title: s,
    text: f(l),
  };
}
function I(u, n) {
  const e = u.match(/^(\s+)(?:```)/);
  if (e === null) return n;
  const t = e[1];
  return n
    .split(
      `
`,
    )
    .map(i => {
      const s = i.match(/^\s+/);
      if (s === null) return i;
      const [l] = s;
      return l.length >= t.length ? i.slice(t.length) : i;
    }).join(`
`);
}
class _ {
  // set by the lexer
  constructor(n) {
    m(this, 'options');
    m(this, 'rules');
    // set by the lexer
    m(this, 'lexer');
    this.options = n || S;
  }
  space(n) {
    const e = this.rules.block.newline.exec(n);
    if (e && e[0].length > 0)
      return {
        type: 'space',
        raw: e[0],
      };
  }
  code(n) {
    const e = this.rules.block.code.exec(n);
    if (e) {
      const t = e[0].replace(/^ {1,4}/gm, '');
      return {
        type: 'code',
        raw: e[0],
        codeBlockStyle: 'indented',
        text: this.options.pedantic
          ? t
          : d(
              t,
              `
`,
            ),
      };
    }
  }
  fences(n) {
    const e = this.rules.block.fences.exec(n);
    if (e) {
      const t = e[0],
        i = I(t, e[3] || '');
      return {
        type: 'code',
        raw: t,
        lang: e[2]
          ? e[2].trim().replace(this.rules.inline.anyPunctuation, '$1')
          : e[2],
        text: i,
      };
    }
  }
  heading(n) {
    const e = this.rules.block.heading.exec(n);
    if (e) {
      let t = e[2].trim();
      if (/#$/.test(t)) {
        const i = d(t, '#');
        (this.options.pedantic || !i || / $/.test(i)) && (t = i.trim());
      }
      return {
        type: 'heading',
        raw: e[0],
        depth: e[1].length,
        text: t,
        tokens: this.lexer.inline(t),
      };
    }
  }
  hr(n) {
    const e = this.rules.block.hr.exec(n);
    if (e)
      return {
        type: 'hr',
        raw: e[0],
      };
  }
  blockquote(n) {
    const e = this.rules.block.blockquote.exec(n);
    if (e) {
      const t = d(
          e[0].replace(/^ *>[ \t]?/gm, ''),
          `
`,
        ),
        i = this.lexer.state.top;
      this.lexer.state.top = !0;
      const s = this.lexer.blockTokens(t);
      return (
        (this.lexer.state.top = i),
        {
          type: 'blockquote',
          raw: e[0],
          tokens: s,
          text: t,
        }
      );
    }
  }
  list(n) {
    let e = this.rules.block.list.exec(n);
    if (e) {
      let t = e[1].trim();
      const i = t.length > 1,
        s = {
          type: 'list',
          raw: '',
          ordered: i,
          start: i ? +t.slice(0, -1) : '',
          loose: !1,
          items: [],
        };
      (t = i ? `\\d{1,9}\\${t.slice(-1)}` : `\\${t}`),
        this.options.pedantic && (t = i ? t : '[*+-]');
      const l = new RegExp(`^( {0,3}${t})((?:[	 ][^\\n]*)?(?:\\n|$))`);
      let o = '',
        r = '',
        x = !1;
      for (; n; ) {
        let h = !1;
        if (!(e = l.exec(n)) || this.rules.block.hr.test(n)) break;
        (o = e[0]), (n = n.substring(o.length));
        let p = e[2]
            .split(
              `
`,
              1,
            )[0]
            .replace(/^\t+/, w => ' '.repeat(3 * w.length)),
          c = n.split(
            `
`,
            1,
          )[0],
          a = 0;
        this.options.pedantic
          ? ((a = 2), (r = p.trimStart()))
          : ((a = e[2].search(/[^ ]/)),
            (a = a > 4 ? 1 : a),
            (r = p.slice(a)),
            (a += e[1].length));
        let g = !1;
        if (
          (!p &&
            /^ *$/.test(c) &&
            ((o +=
              c +
              `
`),
            (n = n.substring(c.length + 1)),
            (h = !0)),
          !h)
        ) {
          const w = new RegExp(
              `^ {0,${Math.min(
                3,
                a - 1,
              )}}(?:[*+-]|\\d{1,9}[.)])((?:[ 	][^\\n]*)?(?:\\n|$))`,
            ),
            $ = new RegExp(
              `^ {0,${Math.min(
                3,
                a - 1,
              )}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`,
            ),
            L = new RegExp(`^ {0,${Math.min(3, a - 1)}}(?:\`\`\`|~~~)`),
            R = new RegExp(`^ {0,${Math.min(3, a - 1)}}#`);
          for (; n; ) {
            const b = n.split(
              `
`,
              1,
            )[0];
            if (
              ((c = b),
              this.options.pedantic &&
                (c = c.replace(/^ {1,4}(?=( {4})*[^ ])/g, '  ')),
              L.test(c) || R.test(c) || w.test(c) || $.test(n))
            )
              break;
            if (c.search(/[^ ]/) >= a || !c.trim())
              r +=
                `
` + c.slice(a);
            else {
              if (
                g ||
                p.search(/[^ ]/) >= 4 ||
                L.test(p) ||
                R.test(p) ||
                $.test(p)
              )
                break;
              r +=
                `
` + c;
            }
            !g && !c.trim() && (g = !0),
              (o +=
                b +
                `
`),
              (n = n.substring(b.length + 1)),
              (p = c.slice(a));
          }
        }
        s.loose || (x ? (s.loose = !0) : /\n *\n *$/.test(o) && (x = !0));
        let k = null,
          y;
        this.options.gfm &&
          ((k = /^\[[ xX]\] /.exec(r)),
          k && ((y = k[0] !== '[ ] '), (r = r.replace(/^\[[ xX]\] +/, '')))),
          s.items.push({
            type: 'list_item',
            raw: o,
            task: !!k,
            checked: y,
            loose: !1,
            text: r,
            tokens: [],
          }),
          (s.raw += o);
      }
      (s.items[s.items.length - 1].raw = o.trimEnd()),
        (s.items[s.items.length - 1].text = r.trimEnd()),
        (s.raw = s.raw.trimEnd());
      for (let h = 0; h < s.items.length; h++)
        if (
          ((this.lexer.state.top = !1),
          (s.items[h].tokens = this.lexer.blockTokens(s.items[h].text, [])),
          !s.loose)
        ) {
          const p = s.items[h].tokens.filter(a => a.type === 'space'),
            c = p.length > 0 && p.some(a => /\n.*\n/.test(a.raw));
          s.loose = c;
        }
      if (s.loose)
        for (let h = 0; h < s.items.length; h++) s.items[h].loose = !0;
      return s;
    }
  }
  html(n) {
    const e = this.rules.block.html.exec(n);
    if (e)
      return {
        type: 'html',
        block: !0,
        raw: e[0],
        pre: e[1] === 'pre' || e[1] === 'script' || e[1] === 'style',
        text: e[0],
      };
  }
  def(n) {
    const e = this.rules.block.def.exec(n);
    if (e) {
      const t = e[1].toLowerCase().replace(/\s+/g, ' '),
        i = e[2]
          ? e[2]
              .replace(/^<(.*)>$/, '$1')
              .replace(this.rules.inline.anyPunctuation, '$1')
          : '',
        s = e[3]
          ? e[3]
              .substring(1, e[3].length - 1)
              .replace(this.rules.inline.anyPunctuation, '$1')
          : e[3];
      return {
        type: 'def',
        tag: t,
        raw: e[0],
        href: i,
        title: s,
      };
    }
  }
  table(n) {
    const e = this.rules.block.table.exec(n);
    if (!e || !/[:|]/.test(e[2])) return;
    const t = B(e[1]),
      i = e[2].replace(/^\||\| *$/g, '').split('|'),
      s =
        e[3] && e[3].trim()
          ? e[3].replace(/\n[ \t]*$/, '').split(`
`)
          : [],
      l = {
        type: 'table',
        raw: e[0],
        header: [],
        align: [],
        rows: [],
      };
    if (t.length === i.length) {
      for (const o of i)
        /^ *-+: *$/.test(o)
          ? l.align.push('right')
          : /^ *:-+: *$/.test(o)
            ? l.align.push('center')
            : /^ *:-+ *$/.test(o)
              ? l.align.push('left')
              : l.align.push(null);
      for (const o of t)
        l.header.push({
          text: o,
          tokens: this.lexer.inline(o),
        });
      for (const o of s)
        l.rows.push(
          B(o, l.header.length).map(r => ({
            text: r,
            tokens: this.lexer.inline(r),
          })),
        );
      return l;
    }
  }
  lheading(n) {
    const e = this.rules.block.lheading.exec(n);
    if (e)
      return {
        type: 'heading',
        raw: e[0],
        depth: e[2].charAt(0) === '=' ? 1 : 2,
        text: e[1],
        tokens: this.lexer.inline(e[1]),
      };
  }
  paragraph(n) {
    const e = this.rules.block.paragraph.exec(n);
    if (e) {
      const t =
        e[1].charAt(e[1].length - 1) ===
        `
`
          ? e[1].slice(0, -1)
          : e[1];
      return {
        type: 'paragraph',
        raw: e[0],
        text: t,
        tokens: this.lexer.inline(t),
      };
    }
  }
  text(n) {
    const e = this.rules.block.text.exec(n);
    if (e)
      return {
        type: 'text',
        raw: e[0],
        text: e[0],
        tokens: this.lexer.inline(e[0]),
      };
  }
  escape(n) {
    const e = this.rules.inline.escape.exec(n);
    if (e)
      return {
        type: 'escape',
        raw: e[0],
        text: f(e[1]),
      };
  }
  tag(n) {
    const e = this.rules.inline.tag.exec(n);
    if (e)
      return (
        !this.lexer.state.inLink && /^<a /i.test(e[0])
          ? (this.lexer.state.inLink = !0)
          : this.lexer.state.inLink &&
            /^<\/a>/i.test(e[0]) &&
            (this.lexer.state.inLink = !1),
        !this.lexer.state.inRawBlock &&
        /^<(pre|code|kbd|script)(\s|>)/i.test(e[0])
          ? (this.lexer.state.inRawBlock = !0)
          : this.lexer.state.inRawBlock &&
            /^<\/(pre|code|kbd|script)(\s|>)/i.test(e[0]) &&
            (this.lexer.state.inRawBlock = !1),
        {
          type: 'html',
          raw: e[0],
          inLink: this.lexer.state.inLink,
          inRawBlock: this.lexer.state.inRawBlock,
          block: !1,
          text: e[0],
        }
      );
  }
  link(n) {
    const e = this.rules.inline.link.exec(n);
    if (e) {
      const t = e[2].trim();
      if (!this.options.pedantic && /^</.test(t)) {
        if (!/>$/.test(t)) return;
        const l = d(t.slice(0, -1), '\\');
        if ((t.length - l.length) % 2 === 0) return;
      } else {
        const l = M(e[2], '()');
        if (l > -1) {
          const r = (e[0].indexOf('!') === 0 ? 5 : 4) + e[1].length + l;
          (e[2] = e[2].substring(0, l)),
            (e[0] = e[0].substring(0, r).trim()),
            (e[3] = '');
        }
      }
      let i = e[2],
        s = '';
      if (this.options.pedantic) {
        const l = /^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(i);
        l && ((i = l[1]), (s = l[3]));
      } else s = e[3] ? e[3].slice(1, -1) : '';
      return (
        (i = i.trim()),
        /^</.test(i) &&
          (this.options.pedantic && !/>$/.test(t)
            ? (i = i.slice(1))
            : (i = i.slice(1, -1))),
        C(
          e,
          {
            href: i && i.replace(this.rules.inline.anyPunctuation, '$1'),
            title: s && s.replace(this.rules.inline.anyPunctuation, '$1'),
          },
          e[0],
          this.lexer,
        )
      );
    }
  }
  reflink(n, e) {
    let t;
    if (
      (t = this.rules.inline.reflink.exec(n)) ||
      (t = this.rules.inline.nolink.exec(n))
    ) {
      const i = (t[2] || t[1]).replace(/\s+/g, ' '),
        s = e[i.toLowerCase()];
      if (!s) {
        const l = t[0].charAt(0);
        return {
          type: 'text',
          raw: l,
          text: l,
        };
      }
      return C(t, s, t[0], this.lexer);
    }
  }
  emStrong(n, e, t = '') {
    let i = this.rules.inline.emStrongLDelim.exec(n);
    if (!i || (i[3] && t.match(/[\p{L}\p{N}]/u))) return;
    if (!(i[1] || i[2] || '') || !t || this.rules.inline.punctuation.exec(t)) {
      const l = [...i[0]].length - 1;
      let o,
        r,
        x = l,
        h = 0;
      const p =
        i[0][0] === '*'
          ? this.rules.inline.emStrongRDelimAst
          : this.rules.inline.emStrongRDelimUnd;
      for (
        p.lastIndex = 0, e = e.slice(-1 * n.length + l);
        (i = p.exec(e)) != null;

      ) {
        if (((o = i[1] || i[2] || i[3] || i[4] || i[5] || i[6]), !o)) continue;
        if (((r = [...o].length), i[3] || i[4])) {
          x += r;
          continue;
        } else if ((i[5] || i[6]) && l % 3 && !((l + r) % 3)) {
          h += r;
          continue;
        }
        if (((x -= r), x > 0)) continue;
        r = Math.min(r, r + x + h);
        const c = [...i[0]][0].length,
          a = n.slice(0, l + i.index + c + r);
        if (Math.min(l, r) % 2) {
          const k = a.slice(1, -1);
          return {
            type: 'em',
            raw: a,
            text: k,
            tokens: this.lexer.inlineTokens(k),
          };
        }
        const g = a.slice(2, -2);
        return {
          type: 'strong',
          raw: a,
          text: g,
          tokens: this.lexer.inlineTokens(g),
        };
      }
    }
  }
  codespan(n) {
    const e = this.rules.inline.code.exec(n);
    if (e) {
      let t = e[2].replace(/\n/g, ' ');
      const i = /[^ ]/.test(t),
        s = /^ /.test(t) && / $/.test(t);
      return (
        i && s && (t = t.substring(1, t.length - 1)),
        (t = f(t, !0)),
        {
          type: 'codespan',
          raw: e[0],
          text: t,
        }
      );
    }
  }
  br(n) {
    const e = this.rules.inline.br.exec(n);
    if (e)
      return {
        type: 'br',
        raw: e[0],
      };
  }
  del(n) {
    const e = this.rules.inline.del.exec(n);
    if (e)
      return {
        type: 'del',
        raw: e[0],
        text: e[2],
        tokens: this.lexer.inlineTokens(e[2]),
      };
  }
  autolink(n) {
    const e = this.rules.inline.autolink.exec(n);
    if (e) {
      let t, i;
      return (
        e[2] === '@'
          ? ((t = f(e[1])), (i = 'mailto:' + t))
          : ((t = f(e[1])), (i = t)),
        {
          type: 'link',
          raw: e[0],
          text: t,
          href: i,
          tokens: [
            {
              type: 'text',
              raw: t,
              text: t,
            },
          ],
        }
      );
    }
  }
  url(n) {
    var t;
    let e;
    if ((e = this.rules.inline.url.exec(n))) {
      let i, s;
      if (e[2] === '@') (i = f(e[0])), (s = 'mailto:' + i);
      else {
        let l;
        do
          (l = e[0]),
            (e[0] =
              ((t = this.rules.inline._backpedal.exec(e[0])) == null
                ? void 0
                : t[0]) ?? '');
        while (l !== e[0]);
        (i = f(e[0])), e[1] === 'www.' ? (s = 'http://' + e[0]) : (s = e[0]);
      }
      return {
        type: 'link',
        raw: e[0],
        text: i,
        href: s,
        tokens: [
          {
            type: 'text',
            raw: i,
            text: i,
          },
        ],
      };
    }
  }
  inlineText(n) {
    const e = this.rules.inline.text.exec(n);
    if (e) {
      let t;
      return (
        this.lexer.state.inRawBlock ? (t = e[0]) : (t = f(e[0])),
        {
          type: 'text',
          raw: e[0],
          text: t,
        }
      );
    }
  }
}
export { _ as _Tokenizer };
